class CompleteDiagnosisPipeline:
    def __init__(self, config):
        self.config = config
        self.preprocessor = None
        self.feature_extractor = None
        self.model = None
        self.xai_module = None
        
    def build_pipeline(self):
        """构建完整管道"""
        # 初始化各模块
        self.preprocessor = DataPreprocessor(self.config['data_path'])
        self.feature_extractor = FeatureExtractor()
        self.model = self._build_model()
        self.xai_module = ExplainableAIMethods(
            self.model, 
            self.config['feature_names'], 
            self.config['class_names']
        )
        
    def _build_model(self):
        """根据配置构建模型"""
        if self.config['model_type'] == 'cnn':
            return DeepLearningModels(
                self.config['input_shape'], 
                self.config['num_classes']
            ).build_cnn_model()
        elif self.config['model_type'] == 'transformer':
            return DeepLearningModels(
                self.config['input_shape'], 
                self.config['num_classes']
            ).build_transformer_model()
        # 其他模型类型...
        
    def run_pipeline(self, source_files, target_files):
        """运行完整管道"""
        results = {}
        
        # 1. 数据加载与预处理
        print("Step 1: Loading and preprocessing data...")
        source_data, source_info = self.preprocessor.load_mat_files(source_files)
        target_data, target_info = self.preprocessor.load_mat_files(target_files)
        
        # 2. 特征提取
        print("Step 2: Extracting features...")
        source_features = self._extract_features_batch(source_data)
        target_features = self._extract_features_batch(target_data)
        
        # 3. 模型训练（源域）
        print("Step 3: Training source domain model...")
        self._train_source_model(source_features)
        
        # 4. 迁移学习
        print("Step 4: Performing transfer learning...")
        self._perform_transfer_learning(source_features, target_features)
        
        # 5. 目标域诊断
        print("Step 5: Diagnosing target domain...")
        diagnosis_results = self._diagnose_target_domain(target_features)
        
        # 6. 可解释性分析
        print("Step 6: Performing explainability analysis...")
        explainability_results = self._perform_explainability_analysis(
            source_features, target_features, diagnosis_results
        )
        
        results.update({
            'diagnosis_results': diagnosis_results,
            'explainability_results': explainability_results,
            'source_info': source_info,
            'target_info': target_info
        })
        
        return results
    
    def _extract_features_batch(self, data_list):
        """批量提取特征"""
        features_list = []
        for data in tqdm(data_list, desc="Extracting features"):
            features = self.feature_extractor.extract_all_features(
                data['de_signal'], data['rpm']
            )
            features_list.append(features)
        return pd.DataFrame(features_list)
    
    def _train_source_model(self, features):
        """训练源域模型"""
        # 实现训练逻辑
        pass
    
    def _perform_transfer_learning(self, source_features, target_features):
        """执行迁移学习"""
        # 实现迁移学习逻辑
        pass
    
    def _diagnose_target_domain(self, target_features):
        """诊断目标域"""
        # 实现诊断逻辑
        pass
    
    def _perform_explainability_analysis(self, source_features, target_features, diagnosis_results):
        """执行可解释性分析"""
        # 实现可解释性分析逻辑
        pass

# 配置管道
config = {
    'data_path': 'path/to/data',
    'model_type': 'hybrid',
    'input_shape': (30, 1),  # 示例
    'num_classes': 4,
    'feature_names': feature_names,
    'class_names': class_names
}

pipeline = CompleteDiagnosisPipeline(config)
pipeline.build_pipeline()

# 运行管道
source_files = [f for f in os.listdir(config['data_path']) if f.startswith('source')]
target_files = [f for f in os.listdir(config['data_path']) if f.startswith('target')]

results = pipeline.run_pipeline(source_files[:50], target_files[:16])
